package com.molichuxing.gateway.bff.permission.convertor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.molichuxing.framework.utils.CollectionUtils;
import com.molichuxing.gateway.bff.permission.entity.request.create.OperationLogCreateVo;
import com.molichuxing.gateway.bff.permission.entity.response.AccountCreateVo;
import com.molichuxing.gateway.bff.permission.entity.response.AccountLoginLogVo;
import com.molichuxing.gateway.bff.permission.entity.response.AccountLoginVo;
import com.molichuxing.gateway.bff.permission.entity.response.AccountModifyVo;
import com.molichuxing.gateway.bff.permission.entity.response.AccountVo;
import com.molichuxing.gateway.bff.permission.entity.response.DesenseVo;
import com.molichuxing.gateway.bff.permission.entity.response.GroupCreateVo;
import com.molichuxing.gateway.bff.permission.entity.response.GroupModifyVo;
import com.molichuxing.gateway.bff.permission.entity.response.GroupVo;
import com.molichuxing.gateway.bff.permission.entity.response.IovAccountCreateVo;
import com.molichuxing.gateway.bff.permission.entity.response.IovAccountModifyVo;
import com.molichuxing.gateway.bff.permission.entity.response.OperationLogVo;
import com.molichuxing.gateway.bff.permission.entity.response.TabVo;
import com.molichuxing.gateway.utils.entity.UserEntity;
import com.molichuxing.services.business.dto.response.AccountLoginBizDto;
import com.molichuxing.services.infrastructure.dto.response.IovGroupDto;
import com.molichuxing.services.infrastructure.dto.response.IovResourceDto;
import com.molichuxing.services.infrastructure.dto.response.request.create.OperationLogCreateDto;
import com.molichuxing.services.infrastructure.dto.response.response.AccountCreateDto;
import com.molichuxing.services.infrastructure.dto.response.response.AccountDto;
import com.molichuxing.services.infrastructure.dto.response.response.AccountModifyDto;
import com.molichuxing.services.infrastructure.dto.response.response.DesenseDto;
import com.molichuxing.services.infrastructure.dto.response.response.GroupCreateDto;
import com.molichuxing.services.infrastructure.dto.response.response.GroupDto;
import com.molichuxing.services.infrastructure.dto.response.response.GroupModifyDto;
import com.molichuxing.services.infrastructure.dto.response.response.LoginLogDto;
import com.molichuxing.services.infrastructure.dto.response.response.OperationLogDto;
import com.molichuxing.services.infrastructure.dto.response.response.ResourceDto;
import com.molichuxing.services.property.GroupSystemTypeEnum;
import com.molichuxing.services.property.SystemTypeEnum;

public class Convert {

    public static UserEntity toUserEntity(AccountLoginBizDto beanBizDto) {
        UserEntity entity = null;
        if (beanBizDto != null) {
            entity = new UserEntity();
            BeanUtils.copyProperties(beanBizDto, entity);
        }
        return entity;
    }

    public static AccountLoginVo toAccountLoginVo(AccountLoginBizDto beanBizDto) {
        AccountLoginVo beanVo = null;
        if (beanBizDto != null) {
            beanVo = new AccountLoginVo();
            BeanUtils.copyProperties(beanBizDto, beanVo);
        }
        return beanVo;
    }

    public static AccountVo toAccountVo(AccountDto beanDto) {
        AccountVo beanVo = null;
        if (beanDto != null) {
            beanVo = new AccountVo();
            BeanUtils.copyProperties(beanDto, beanVo);

            if (beanDto.getSystemType() != null) {
                beanVo.setSystemType(beanDto.getSystemType().getValue());
                beanVo.setSystemTypeStr(beanDto.getSystemType().getName());
            }
            beanVo.setIsSuper(beanDto.getIsSuper() ? 1 : 0);
            beanVo.setSmartTerminal(beanDto.getSmartTerminal() ? 1 : 0);
        }
        return beanVo;
    }

    public static AccountDto toAccountDto(AccountVo vo) {
        AccountDto dto = null;
        if (vo != null) {
            dto = new AccountDto();
            BeanUtils.copyProperties(vo, dto);
        }
        return dto;
    }

    public static List<GroupVo> toGroupVoList(List<GroupDto> dtoList) {
        List<GroupVo> result = null;
        if (CollectionUtils.isNotEmpty(dtoList)) {
            result = new ArrayList<>();
            for (GroupDto dto : dtoList) {
                result.add(toGroupVo(dto));
            }
        }
        return result;
    }

    public static GroupVo toGroupVo(GroupDto dto) {
        GroupVo vo = null;
        if (dto != null) {
            vo = new GroupVo();
            BeanUtils.copyProperties(dto, vo);
            if (dto.getGroupSystemType() != null) {
                vo.setGroupSystemType(dto.getGroupSystemType().getValue());
                vo.setSystemTypeStr(dto.getGroupSystemType().getName());
            }
        }
        return vo;
    }

    public  static GroupDto toGroupDto(GroupVo vo) {
        GroupDto dto = null;
        if (vo != null) {
            dto = new GroupDto();
            BeanUtils.copyProperties(vo, dto);
            if (vo.getGroupSystemType() != null) {
                dto.setGroupSystemType(GroupSystemTypeEnum.getEnum(vo.getGroupSystemType()));
            }
        }
        return dto;
    }

    public static List<DesenseVo> toDesenseVoList(List<DesenseDto> dtos) {
        List<DesenseVo> vos = null;
        if (CollectionUtils.isNotEmpty(dtos)) {
            vos = new ArrayList<>();
            for (DesenseDto dto : dtos) {
                vos.add(toDesenseVo(dto));
            }
        }
        return  vos;
    }

    public static DesenseVo toDesenseVo(DesenseDto dto) {
        DesenseVo vo = null;
        if (dto != null) {
            vo = new DesenseVo();
            BeanUtils.copyProperties(dto, vo);
        }
        return vo;
    }

    public static AccountCreateVo IovCreateAccountVoToAccountCreateVo(IovAccountCreateVo iov) {
        AccountCreateVo vo = null;
        if (iov != null) {
            vo = new AccountCreateVo();
            BeanUtils.copyProperties(iov, vo);
            vo.setStatus(iov.getStatus() == 1 ? 0 : 1);
        }
        return vo;
    }

    public static AccountModifyVo IovModifyAccountVoToAccountModifyVo(IovAccountModifyVo iov) {
        AccountModifyVo vo = null;
        if (iov != null) {
            vo = new AccountModifyVo();
            BeanUtils.copyProperties(iov, vo);
        }
        return vo;
    }

    public static AccountCreateDto toAccountCreateDto(AccountCreateVo vo) {
        AccountCreateDto dto = null;
        if (vo != null) {
            dto = new AccountCreateDto();
            BeanUtils.copyProperties(vo, dto);
            if (vo.getSmartTerminal() == 1) {
                dto.setSmartTerminal(true);
            } else {
                dto.setSmartTerminal(false);
            }
        }
        return dto;
    }

    public static AccountModifyDto toAccountModifyDto(AccountModifyVo vo) {
        AccountModifyDto dto = null;
        if (vo != null) {
            dto = new AccountModifyDto();
            BeanUtils.copyProperties(vo, dto);
            if (vo.getSmartTerminal() != null && vo.getSmartTerminal() == 1) {
                dto.setSmartTerminal(true);
            } else {
                dto.setSmartTerminal(false);
            }
        }
        return dto;
    }

    public static AccountDto toAccountDto(IovAccountModifyVo vo) {
        AccountDto dto = null;
        if (vo != null) {
            dto = new AccountDto();
            BeanUtils.copyProperties(vo, dto);
            dto.setStatus(vo.getStatus() == 0 ? 1 : 0);
            dto.setSystemType(SystemTypeEnum.OP.getValue());
        }
        return dto;
    }

    public static GroupCreateDto toGroupCreateDto(GroupCreateVo vo) {
        GroupCreateDto dto = null;
        if (vo != null) {
            dto = new GroupCreateDto();
            BeanUtils.copyProperties(vo, dto);
        }
        return dto;
    }

    public static GroupModifyDto toGroupModifyDto(GroupModifyVo vo) {
        GroupModifyDto dto = null;
        if (vo != null) {
            dto = new GroupModifyDto();
            BeanUtils.copyProperties(vo, dto);
        }
        return dto;
    }

    public static List<GroupVo> iovGroupListToGroupVoList(List<IovGroupDto> dtoList) {
        List<GroupVo> result = null;
        if (CollectionUtils.isNotEmpty(dtoList)) {
            result = new ArrayList<>();
            for (IovGroupDto dto : dtoList) {
                result.add(iovGroupToGroupVo(dto));
            }
        }
        return result;
    }

    public static GroupVo iovGroupToGroupVo(IovGroupDto dto) {
        GroupVo vo = null;
        if (dto != null) {
            vo = new GroupVo();
            vo.setId(dto.getRoleId());
            vo.setGroupName(dto.getRoleName());
            vo.setStatus(dto.getStatus() == 1 ? 0 : 1);
            vo.setGroupDesc(dto.getMemo());
            vo.setSelected(dto.getSelected());
            vo.setGroupSystemType(GroupSystemTypeEnum.IOV.getValue());
            vo.setSystemTypeStr(GroupSystemTypeEnum.IOV.getName());
            vo.setGmtCreate(dto.getCreateTime());
            vo.setOperatorAccount(dto.getOperatorName());
        }
        return vo;
    }

    public static  List<ResourceDto> toResourceDtoList(List<IovResourceDto> dtos) {
        List<ResourceDto> vos = null;
        if (CollectionUtils.isNotEmpty(dtos)) {
            vos = new ArrayList<>();
            for (IovResourceDto dto : dtos) {
                vos.add(toResourceDto(dto));
            }
        }
        return vos;
    }

    public static ResourceDto toResourceDto(IovResourceDto dto) {
        ResourceDto vo = null;
        if (dto != null) {
            vo = new ResourceDto();
            BeanUtils.copyProperties(dto, vo);
        }
        return vo;
    }

    public static List<AccountLoginLogVo> toAccountLoginLogVo(List<LoginLogDto> dtoList) {
        List<AccountLoginLogVo> voList = null;
        if (dtoList != null && dtoList.size() > 0) {
            voList = new ArrayList<>();
            for (LoginLogDto dto : dtoList) {
                voList.add(toAccountLoginLogVo(dto));
            }
        }

        return voList;
    }

    private static AccountLoginLogVo toAccountLoginLogVo(LoginLogDto dto) {
        AccountLoginLogVo vo = null;
        if (dto != null) {
            vo = new AccountLoginLogVo();
            BeanUtils.copyProperties(dto, vo);
            vo.setResultStr(vo.getResult() ? "成功" : "失败");
        }

        return vo;
    }


    public static List<TabVo> toTabVo(List<ResourceDto> dtoList) {
        List<TabVo> voList = null;
        if (dtoList != null && dtoList.size() > 0) {
            voList = new ArrayList<>();
            for (ResourceDto dto : dtoList) {
                voList.add(toTabVo(dto));
            }
        }

        return voList;
    }

    private static TabVo toTabVo(ResourceDto dto) {
        TabVo vo = null;
        if (dto != null) {
            vo = new TabVo();
            vo.setType(dto.getSort());
            vo.setName(dto.getPermissionName());
        }

        return vo;
    }
    
    public static OperationLogCreateDto toOperationLogCreateDto(OperationLogCreateVo createVo) {
		OperationLogCreateDto entity = null;
		if (createVo != null) {
			entity = new OperationLogCreateDto();
			BeanUtils.copyProperties(createVo, entity);
		}
		return entity;
	}
    
    public static List<OperationLogVo> toOperationLogVoPage(List<OperationLogDto> beanList) {
		List<OperationLogVo> dtoList = null;;
		if (beanList != null && beanList.size() > 0) {
			dtoList = new ArrayList<>();
			for (OperationLogDto bean : beanList) {
				dtoList.add(toOperationLogVoPage(bean));
			}
		}
		return dtoList;
	}

	public static OperationLogVo toOperationLogVoPage(OperationLogDto bean) {
		OperationLogVo beanDto = null;
		if (bean != null) {
			beanDto = new OperationLogVo();
			BeanUtils.copyProperties(bean, beanDto);
		}
		return beanDto;
	}
}
